/*///////////////////////////////////////////////////////////////////////////////
-- Author:      gyz
-- Created:     Thu Sep 25 09:48:31 CST 2008
-- Description:	    
-- If you have any question,please email to gaoyz@gentlesoft.com.cn 
////////////////////////////////////////////////////////////////////////////////*/
package com.ctsi.security.service;

import com.ctsi.commons.event.BaseEvent;
import com.ctsi.commons.event.EventPublisherMan;
import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.security.domain.UserDataObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户数据权限 Service 类
 *
 * @author zhangjw
 * @description 用户数据权限
 * @created Thu Sep 25 09:48:31 CST 2018
 */
@SuppressWarnings("unchecked")
public class UserDataObjectService {
    private static final Logger logger = LoggerFactory.getLogger(UserDataObjectService.class);
    private static String INSERTUSERDATAOBJECT = "INSERTUSERDATAOBJECT";
    private static String REMOVEUSERDATAOBJECT = "REMOVEUSERDATAOBJECT";
    private MybatisGenericDao dao;
    private EventPublisherMan eventMan;

    public void setDao(MybatisGenericDao dao) {
        this.dao = dao;
    }

    /**
     * 更新或添加
     */
    public void update(UserDataObject userDataObject) {
        logger.debug("update {}", userDataObject);
        dao.update(userDataObject);
    }

    /**
     * 添加
     */
    public void insert(UserDataObject userDataObject) {
        userDataObject.setId(UUIDGenerator.getId());
        logger.debug("insert {}", userDataObject);
//		发布增加UserDataObject事件
        BaseEvent event = new BaseEvent(userDataObject, INSERTUSERDATAOBJECT);
        eventMan.getEventPublisher().publishEvent(event);
        dao.insert(userDataObject);
    }

    /**
     * 动态更新
     */
    public void updateByPrimaryKeySelective(UserDataObject userDataObject) {
        logger.debug("updateByPrimaryKeySelective {}", userDataObject);
        dao.update(UserDataObject.class.getName() + ".updateByPrimaryKeySelective", userDataObject);
    }

    /**
     * 根据ID编辑 Demo对象
     */
    public UserDataObject editById(String id) {
        if (UtilValidate.isEmpty(id))
            return null;
        logger.debug("editById {}", id);
        return (UserDataObject) dao.queryForObject(UserDataObject.class.getName() + ".edit", id);
    }

    /**
     * 根据ID查找 Demo对象
     */
    public UserDataObject findById(String id) {
        if (UtilValidate.isEmpty(id))
            return null;
        logger.debug("findById {}", id);
        return dao.get(UserDataObject.class, id);
    }

    /**
     * 删除Demo对象
     */
    public void remove(UserDataObject userDataObject) {
        if (UtilValidate.isNotEmpty(userDataObject.getId()))
            dao.remove(userDataObject);
//		发布删除userDataObject事件
        BaseEvent event = new BaseEvent(userDataObject, REMOVEUSERDATAOBJECT);
        eventMan.getEventPublisher().publishEvent(event);
    }

    /**
     * 详细页查询
     */
    public Map findByDetail(String id) {
        return (Map) dao.queryForObject(UserDataObject.class.getName() + ".findByDetail", id);
    }

    /**
     * 多条件查询结果
     */
    public Page<UserDataObject> findByMulitCondition(Map param, int start, int pageSize) {
        logger.debug("findByMulitCondition {}", param);
        if (pageSize < 0 || pageSize > 200)
            pageSize = Page.DEFAULT_PAGE_SIZE;
        if (start != -1)
            return dao.queryPage(UserDataObject.class.getName() + ".selectMulitCondition", param, start, pageSize);
        else {
            List list = dao.queryForList(UserDataObject.class.getName() + ".selectMulitCondition", param);
            if (list != null)
                return new Page<UserDataObject>(list, list.size());
            else
                return new Page<UserDataObject>();
        }
    }

    /**
     * 查询全部 分页
     */
    public Page queryPage(int start) {
        logger.debug("queryPage {}", start);
        return dao.queryPage(UserDataObject.class.getName() + ".select", null, start, Page.DEFAULT_PAGE_SIZE);
    }

    public void setEventMan(EventPublisherMan eventMan) {
        this.eventMan = eventMan;
    }

    public void removeAllResByUserId(String userId) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(UserDataObject.class.getName());
        stringBuilder.append(".delAllUserRes");
        dao.delete(stringBuilder.toString(), userId);
    }

    public UserDataObject findUserDataObjectById(String id) {
        return findById(id);
    }

    public List<UserDataObject> findUserDataObjectByMap(Map<String, Object> params) {
        return findByMulitCondition(params, -1, 0).getResult();
    }

    public List<String> getActions(String userLogin, String resType, String resId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userLogin);
        params.put("resType", resType);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(UserDataObject.class.getName());
        stringBuilder.append(".queryByResType");
        List<UserDataObject> list = (List<UserDataObject>) dao.queryForList(stringBuilder.toString(), params);
        return getActionsFromList(list);
    }

    private List<String> getActionsFromList(List<UserDataObject> list) {
        List<String> result = new ArrayList<String>();
        for (UserDataObject item : list) {
            result.add(item.getAction());
        }
        return result;
    }

    public List<UserDataObject> getUserDataObjects(String userLogin, String resType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userLogin);
        params.put("resType", resType);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(UserDataObject.class.getName());
        stringBuilder.append(".queryByResType");
        return (List<UserDataObject>) dao.queryForList(stringBuilder.toString(), params);
    }

    public void insertUserDataObject(UserDataObject userDataObject) {
        insert(userDataObject);

    }

    public void removeUserDataObject(UserDataObject userDataObject) {
        remove(userDataObject);

    }

    public void updateUserDataObject(UserDataObject userDataObject) {
        update(userDataObject);

    }

}
